package com.example.sunming.lambda;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.function.*;

/**
 * @author: sunming
 * @date: 2020/3/21
 * @description:
 */
public class TestLambda {

    public static void main(String[] args) {

        List<Mask> list = new ArrayList<>();
        list.add(new Mask("3M", "KN95"));
        list.add(new Mask("3M", "FFP2"));
        list.add(new Mask("Honeywell", "KN95"));
        list.add(new Mask("Honeywell", "N95"));

        // 按照品牌排序
        // java8之前
        list.sort(new Comparator<Mask>() {
            @Override
            public int compare(Mask o1, Mask o2) {
                return o1.getBrand().compareTo(o2.getBrand());
            }
        });

        // java8 两种写法
        list.sort((Mask o1, Mask o2) -> o1.getBrand().compareTo(o2.getBrand()));
        list.sort((Mask o1, Mask o2) -> {
            return o1.getBrand().compareTo(o2.getBrand());
        });

        // 方法引用
        String type = "N95";
//        Predicate<String> predicate = (String str) -> type.equals(str);
        Predicate<String> predicate = type::equals;
        System.out.println(predicate.test("N95"));

        // Supplier函数式接口
        Supplier<Mask> supplier = () -> new Mask("3M", "N95");
        Mask mask = supplier.get();
        System.out.println("Brand:" + mask.getBrand() + ",type:" + mask.getType());

        // Consumer
        Consumer<Mask> consumer = (Mask o) -> {
            System.out.println("Brand:" + o.getBrand() + ",type:" + o.getType());
        };
        consumer.accept(supplier.get());

        // Function<T,R>
        Function<Mask, String> brand = Mask::getBrand;

        System.out.println(brand.apply(supplier.get()));

        // BiFunction<T,U,R>
        BiFunction<String, String, Mask> biFunction = Mask::new;
        Mask apply = biFunction.apply("3M", "N95");
        System.out.println(apply.getBrand());


    }

}
